﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Odelle.Utils
{
    public class DhtmlHandler
    {
        //

        public static int GetInnerInteger(XmlElement element)
        {
            int value=0;
            int.TryParse(GetInnerText(element), out value);
            return value;
        }

        public static double GetInnerDouble(XmlElement element)
        {
            return GetInnerDouble(element,0);
        }

        public static double GetInnerDouble(XmlElement element, int index)
        {
            string eleText=GetInnerText(element).Replace("%", "");
            double value = 0.0;

            string[] text=null;
            
            if (eleText.Contains('/'))
                text=eleText.Split(new char[] {'/'});
            else
                text = new string[] { eleText };

            if (index < text.Length)
                double.TryParse(text[index], out value);

            return value;
        }

        public static string GetInnerText(XmlElement element)
        {
            if (element == null)
                return "";

            string retValue = "";

            retValue = element.InnerText;
            retValue = retValue.Replace("&nbsp;", " ");
            retValue = retValue.Replace("\xA0", " ");
            retValue = retValue.Trim("\r\n\t ".ToCharArray());

            if (retValue == " ")
                retValue = "";

            return retValue;
        }

        public static List<string> ExtractJsonText(string sourceText)
        {
            List<string> destination = new List<string>();

            if (sourceText != "")
            {
                string value = "";
                int count = 0;

                for (int i = 0; i < sourceText.Length; ++i)
                {
                    string buffer = sourceText.Substring(i);

                    switch (buffer)
                    {
                        case "{":
                            ++count;
                            value += buffer;
                            break;
                        case "}":
                            if (count > 0)
                            {
                                value += buffer;
                                --count;

                                if (count == 0)
                                {
                                    destination.Add(value);
                                    value = "";
                                }
                            }

                            break;
                        default:
                            if (count > 0)
                            {
                                value += buffer;
                            }
                            break;
                    }
                }

            }

            return destination;
        }
    }
}
